package com.yizisu.playerlibrary.view.video_view.view

import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import com.yizisu.playerlibrary.R
import com.yizisu.playerlibrary.view.video_view.helper.IPlayerConfig
import com.yizisu.playerlibrary.view.video_view.helper.IPlayerHelper

/**
 * 当前视频标题文本
 */
open class PlayerFullScreenView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatImageView(
    context,
    attrs,
    defStyleAttr
), IPlayerHelper {

    private fun updateIcon(isFullScreen: Boolean) {
        setImageResource(
            if (isFullScreen)
                R.drawable.exo_ic_fullscreen_exit
            else
                R.drawable.exo_ic_fullscreen_enter
        )
    }


    override fun onAttachPlayerConfig(playerConfig: IPlayerConfig) {
        super.onAttachPlayerConfig(playerConfig)
        playerConfig.fullScreenChangeData.observeForever(::onFullScreenChange)
        setOnClickListener {
            val fullScreen = playerConfig.fullScreenChangeData.value ?: false
            playerConfig.updateFullScreenChange(!fullScreen)
        }
    }


    override fun onDetachedPlayerConfig(playerConfig: IPlayerConfig) {
        super.onDetachedPlayerConfig(playerConfig)
        playerConfig.fullScreenChangeData.removeObserver(::onFullScreenChange)
    }


    private fun onFullScreenChange(fullScreen: Boolean?) {
        updateIcon(fullScreen ?: false)
        if (context is Activity) {
            (context as Activity).window.fullScreen(fullScreen ?: false)
        }
    }
}